/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.dataTypePropGen.assoUdt;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.InterfacePropertyGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.springframework.util.StringUtils;

public class AssoUdtInfoPropertyGenerator extends InterfacePropertyGenerator {

  private final DataTypeDataGeneratorContext dataGenContext;
  private SimpleDataTypeDef udt;
  private IGspCommonField field;
  private boolean convertToUpper = false;

  public AssoUdtInfoPropertyGenerator(
      IGspCommonField field, SimpleDataTypeDef udt, DataTypeDataGeneratorContext dataGenContext) {
    this(field, udt, dataGenContext,false);
  }

  public AssoUdtInfoPropertyGenerator(
          IGspCommonField field, SimpleDataTypeDef udt, DataTypeDataGeneratorContext dataGenContext, boolean convertToUpper) {
    this.udt = udt;
    this.field = field;
    this.convertToUpper = convertToUpper;
    this.dataGenContext = dataGenContext;
  }

  @Override
  protected void afterInitialize() {
    super.afterInitialize();
    TypeInfo type = getPropertyType();
    dataGenContext.getPropertyInfos().put(getPropertyName(),
        new MediatePropertyInfo(getPropertyName()) {{
          setElement(field);
          setPropertyType(type);
        }}
    );
  }

  @Override
  protected String getPropertyName() {
    if(convertToUpper){
      return Utils.toUpperCaseFirstOne(field.getLabelID());
    }
    else {
      return field.getLabelID();
    }

  }

  @Override
  protected TypeInfo getPropertyType() {
    return AssoUdtEnrichedInfoGenerator.getItemTypeInfo(field);
  }

  @Override
  protected String getFieldName() {
    return null;
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    if(convertToUpper)
      return null;
    ArrayList<AnnotationInfo> list = new ArrayList<>();
//    list.add(getPropGetAnnotationInfo());
    return list;
  }

//  private AnnotationInfo getPropGetAnnotationInfo() {
//    if(convertToUpper)
//      return null;
//    AnnotationInfo annotationInfo = new AnnotationInfo();
//    TypeInfo typeInfo = new TypeInfo(PropertyGetAtrribute.class);
//    annotationInfo.setTypeInfo(typeInfo);
//    annotationInfo.setAnnotationType(AnnotationType.Normal);
//    annotationInfo
//        .getParams()
//        .put("propName", ExpressionUtils.getStringLiteral(ast, getPropertyName()));
//    return annotationInfo;
//  }
}
